<?php

namespace App\Http\Controllers\Common;

use App\Models\Common\China;
use App\Models\Constant\OrderConstant;
use App\Models\Order\Order;
use App\Models\Repair\ReapirFactoryComment;
use App\Models\Repair\RepairFactoryAdmin;

use Illuminate\Http\Request;

abstract class ApiController extends CommonController {


    public function detail(Request $request) {
        $id = $request->input('id',0);
        $type = $request->input('type',1);
        //$order = Order::where(compact('id'))->with(['getRepairFactory:id,name,address','owner'])->first();
        $order = Order::where(compact('id'))->with(['getRepairFactory','owner'])->first();
        if(empty($order))
            return responseFail('未查到有效订单！');
        $order->car_jia_number = $order->getCarJiaNumberOfChengBao();
        $order->status_name = OrderConstant::MAP_STATUS[$order->status]??'未知';
        $order->title = !empty($order->car_number)?'车牌号:'.$order->car_number:'车架号:'.$order->car_jia_number;
        if(!empty($order->getRepairFactory)){
            $order->repair_factory_phones = RepairFactoryAdmin::where(['factory_id'=>$order->getRepairFactory->id])->pluck('phone')->implode(',');
            $mapArea = China::getMap();
            $prov =$mapArea[ $order->getRepairFactory->prov_code]??'';
            $city =$mapArea[ $order->getRepairFactory->city_code]??'';
            $county =$mapArea[ $order->getRepairFactory->county_code]??'';

            $order->getRepairFactory->address = $prov.$city.$county.$order->getRepairFactory->address;
        }
        $order->ding_sun_price = $order->ding_sun_price.'元';
        $order->is_show_reapply_btn = false;//重新申请
        $order->is_show_cancel_btn = false;//取消
        $order->is_show_receive_btn = false;//待接单(接单操作)
        $order->is_show_start_repair_btn = false;//待维修(开始维修)
        $order->is_show_end_repair_btn = false;//已完成
        $order->is_show_reupload_btn = false;

        if($type==1){
            $order->is_show_cancel_btn = in_array($order->status,[
                OrderConstant::STATUS_WAIT_AUTO_CHECK,
                OrderConstant::STATUS_WAIT_HUMAN_CHECK,
                OrderConstant::STATUS_CHECK_NOT_PASS,
                OrderConstant::STATUS_CHECK_PASS,
                OrderConstant::STATUS_WAIT_AUTO_HUMAN_CHECK,
                OrderConstant::STATUS_WAIT_OFF,
                OrderConstant::STATUS_WAIT_REPAY,
                OrderConstant::STATUS_WAIT_RECEIVING,
                OrderConstant::STATUS_WAIT_REPAIR,
            ]);

            //当时拒绝状态 && 电子发票字段是空，说明是要显示  重新申请按钮（传定损单）
            $order->is_show_reapply_btn =(in_array($order->status,[
                OrderConstant::STATUS_CHECK_NOT_PASS
            ]) && empty($order->invoice_url));

            //当时拒绝状态 && 电子发票字段是有值的，说明是要显示  重新上传资料
            $order->is_show_reupload_btn =(in_array($order->status,[
                OrderConstant::STATUS_CHECK_NOT_PASS
            ]) && !empty($order->invoice_url));
        }

        if($type==2){
            $order->is_show_receive_btn = in_array($order->status,[
                OrderConstant::STATUS_WAIT_RECEIVING,
            ]);
            $order->is_show_start_repair_btn = in_array($order->status,[
                OrderConstant::STATUS_WAIT_REPAIR,
            ]);
            $order->is_show_end_repair_btn = in_array($order->status,[
                OrderConstant::STATUS_WAIT_REPAIRING,
            ]);
        }

        //如果状态是 已完成，需要加上 当前用户的评论
        $order->myComments = [];
        if($order->status == OrderConstant::STATUS_END){
            $order->myComments = ReapirFactoryComment::where('repair_factory_id',$order->repair_factory_id)
                ->where('user_id',$order->user_id)
                ->with('getUser:id,wx_avatar,wx_nickname')
                ->get()->toArray();
        }
        return responseSuccess($order->toArray());
    }

    public function cancel(Request $request) {
        $id = $request->input('order_id');
        Order::where(compact('id'))->update(['status'=>OrderConstant::STATUS_CANCEL]);
        return responseSuccess();
    }



    protected function get_url($durl, $post_data=[]){
        //请求头配置
        $headers = array(
            "host:recognition.image.myqcloud.com",
            "content-type:application/json",
            "authorization:".getTencentSign()['signStr'], //鉴权算法 下方查看
        );
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $durl);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        //curl_setopt($curl, CURLOPT_POST, true);
        //curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLINFO_HEADER_OUT, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        $data = curl_exec($curl);
        curl_close($curl);
        return $data;
    }

}
